"""
数据库模型
"""
from sqlalchemy import Column, Integer, String, Float, BigInteger, Date, DateTime, Text
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime

Base = declarative_base()

class StockPrice(Base):
    """股票价格表"""
    __tablename__ = "stock_prices"
    
    id = Column(Integer, primary_key=True, index=True)
    symbol = Column(String(20), index=True, nullable=False)  # 股票代码 如 sh600519
    timestamp = Column(BigInteger, nullable=False, index=True)  # 时间戳（毫秒）
    open = Column(Float, nullable=False)  # 开盘价
    high = Column(Float, nullable=False)  # 最高价
    low = Column(Float, nullable=False)  # 最低价
    close = Column(Float, nullable=False)  # 收盘价
    volume = Column(BigInteger, nullable=False)  # 成交量
    created_at = Column(DateTime, default=datetime.now)  # 创建时间
    
    def __repr__(self):
        return f"<StockPrice {self.symbol} @ {self.timestamp}>"


class TradingSignal(Base):
    """交易信号表"""
    __tablename__ = "trading_signals"
    
    id = Column(Integer, primary_key=True, index=True)
    symbol = Column(String(20), index=True, nullable=False)  # 股票代码
    signal_type = Column(String(20), nullable=False)  # 信号类型: BUY, SELL, HOLD, STRONG_BUY, STRONG_SELL
    price = Column(Float, nullable=False)  # 当前价格
    strength = Column(Integer, nullable=False)  # 信号强度 0-100
    reason = Column(Text)  # 信号原因
    target_price = Column(Float)  # 目标价
    stop_loss = Column(Float)  # 止损价
    signal_date = Column(Date, index=True, nullable=False)  # 信号日期
    created_at = Column(DateTime, default=datetime.now)  # 创建时间
    
    def __repr__(self):
        return f"<TradingSignal {self.symbol} {self.signal_type}>"


class TradingPoint(Base):
    """历史买卖点表"""
    __tablename__ = "trading_points"
    
    id = Column(Integer, primary_key=True, index=True)
    symbol = Column(String(20), index=True, nullable=False)  # 股票代码
    point_type = Column(String(10), nullable=False)  # BUY 或 SELL
    timestamp = Column(BigInteger, nullable=False)  # 时间戳
    price = Column(Float, nullable=False)  # 买卖价格
    reason = Column(Text)  # 原因
    profit_rate = Column(Float)  # 盈利率（仅卖出点有值）
    created_at = Column(DateTime, default=datetime.now)  # 创建时间
    
    def __repr__(self):
        return f"<TradingPoint {self.symbol} {self.point_type}>"



